import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import store from "./store/";

import iView from "iview";
import "iview/dist/styles/iview.css";
import "../my-theme/my.css";
// import "../my-theme/index.less"

import echarts from "echarts";
Vue.prototype.$echarts = echarts;

// 在 webpack 环境下指向 vue-echarts/components/ECharts.vue
import ECharts from "vue-echarts";

// 手动导入 ECharts 各模块来减小打包体积
import "echarts/lib/chart/line";
import "echarts/lib/chart/bar";
import "echarts/lib/chart/pie";

// title,legend,tooltip 是 echarts 的组件，
// 在图表中使用需要按组件导入，否则图表的标题图例可能不能显示。
import "echarts/lib/component/tooltip";
import "echarts/lib/component/legend";
import "echarts/lib/component/title";

// 导入自带主题，可以引入多个主题
// import "echarts/theme/dark";
// import "echarts/theme/macarons";
// import "echarts/theme/infographic";

// 自定义主题如果是 js 文件，可以放在 echarts/theme 目录下
// 如果是 json 文件，可以像下面一样使用
// import "echarts/theme/walden";

// 导入自定义主题
// ./views/theme/ 下保存的是自定义的主题
import chartsTheme from "@/theme/chartsTheme.json";
// import wonderland from "@/theme/wonderland.json"

// 注册自定义主题
ECharts.registerTheme("chartsTheme", chartsTheme);
// ECharts.registerTheme("wonderland", wonderland)

// 视频播放 vue-video-player
import VueVideoPlayer from "vue-video-player";
// require videojs style
import "video.js/dist/video-js.css";
// import "vue-video-player/src/custom-theme.css"
//引入图标
import "./assets/iconfont/iconfont.css";

// Vue.use(VueVideoPlayer /* {
//   options: global default options,
//   events: global videojs events
// } */);
Vue.use(VueVideoPlayer);

// 注册组件后即可使用
Vue.component("v-chart", ECharts);

Vue.use(iView);
Vue.config.productionTip = false;

// 路由拦截, 切换路由强制取消上一各页面发起的请求
import { cancelAxiosList } from "./api/ajax";
import { getToken } from '@/util/util'
import constData from "@/util/constData";

const LOGIN_PAGE_NAME = 'login'

router.beforeEach((to, from, next) => {

  console.log("路由:", to, from, constData)

  if (to.query.station) {
    let sta = constData.stationList.find(ele => {
      // console.log('ele:', ele)
      return ele.value === to.query.station;
    });
    console.log('station:', sta)
    let stationData = {
      station: sta.value,
      dateTime: 'today'
    }
    store.dispatch('updateSelectedStationData', stationData)
    // this.selectedData = this.$store.state.selectedStationData
    // console.log('this.selectedData', this.selectedData)
    // this.$router.push({ name: 'station', params: stationData})
    sessionStorage.setItem("station", stationData.station)
  } else {
    let sessionStation = sessionStorage.getItem("station")
    console.log('sessionStation:', sessionStation)
    let stationData = {
      station: sessionStation,
      dateTime: 'today'
    }
    console.log('主页查看站点数据:', stationData)
    store.dispatch('updateSelectedStationData', stationData)

    // this.$router.push({ name: 'station', params: {station:sessionStation, dateTime: 'today'}})
  }

  const token = getToken()
  if (!token && to.name !== LOGIN_PAGE_NAME) {
    // 未登录且要跳转的页面不是登录页
    next({
      name: LOGIN_PAGE_NAME // 跳转到登录页
    })
  } else if (!token && to.name === LOGIN_PAGE_NAME) {
    // 未登陆且要跳转的页面是登录页
    next() // 跳转
  } else if (token && to.name === LOGIN_PAGE_NAME) {
    // 已登录且要跳转的页面是登录页
    console.log('已登录且要跳转的页面是登录页', store.state.selectedStationData)
    // route.push({ name: 'station', params: {station: store.state.selectedStationData, dateTime: 'today'}})
  } else {
    console.log('已登录', store.state.selectedStationData)
    next()
  }
});

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount("#app");
